//
//  SPHomeViewController.m
//  SuperPatrner
//
//  Created by EvanZ on 15/3/15.
//  Copyright (c) 2015年 EvanZ. All rights reserved.
//

#import "SPHomeViewController.h"
#import "SPLoginViewController.h"
#import "QRCodeGenerator.h"
#import "SPHTTPRequestOperationManager.h"
#import "Define.h"
#import "Methods.h"
#import "UIColor+CreateMethod.h"

@interface SPHomeViewController (){
    UIScrollView * m_scrollView;
    UITextView * m_userInfoTextView;
    UITextField * earningTextField;
}

@end

@implementation SPHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSuccess) name:NOTI_LOGIN_SUCC object:nil];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    SPLoginViewController * vc = [[SPLoginViewController alloc]init];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
    nav.navigationBarHidden = YES;
    [self.navigationController presentViewController:nav animated:NO completion:nil];
    
    m_scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    m_scrollView.showsHorizontalScrollIndicator = NO;
    m_scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:m_scrollView];
    
    UIImage * bgImage = [UIImage imageNamed:@"login_bg"];
    UIImageView * bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width/bgImage.size.width*bgImage.size.height)];
    [bgImageView setImage:bgImage];
    [m_scrollView addSubview:bgImageView];
    
    UIImageView * avatarImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 20, 90, 90)];
    [avatarImageView setImage:[UIImage imageNamed:@"avatar"]];
    [m_scrollView addSubview:avatarImageView];
    
    UIView * darkView = [[UIView alloc]initWithFrame:CGRectMake(0, bgImageView.frame.origin.y + bgImageView.frame.size.height - 40, self.view.frame.size.width, 40)];
    [darkView setBackgroundColor:[UIColor colorWithHex:@"#666666" alpha:1]];
    [m_scrollView addSubview:darkView];
    
    earningTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, darkView.frame.origin.y + 7, self.view.frame.size.width - 20, 25)];
    [earningTextField setBackgroundColor:[UIColor clearColor]];
    [earningTextField setFont:[UIFont systemFontOfSize:14]];
    [earningTextField setTextColor:[UIColor whiteColor]];
    [earningTextField setText:@"日增收益：1000.34元   本月收益：38045元"];
    [m_scrollView addSubview:earningTextField];
    
    m_userInfoTextView = [[UITextView alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2, 30, self.view.frame.size.width/2, bgImageView.frame.size.height)];
    [m_userInfoTextView setEditable:NO];
    [m_userInfoTextView setFont:[UIFont systemFontOfSize:15]];
    [m_userInfoTextView setBackgroundColor:[UIColor clearColor]];
    [m_userInfoTextView setText:@"会员ID： \n昵称： \n下级合伙人数：182 \n返还系数A： \n返还系数B："];
    [m_scrollView addSubview:m_userInfoTextView];
    
    UIImageView * buttonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, bgImageView.frame.origin.y + bgImageView.frame.size.height, 320, 277)];
    [buttonView setImage:[UIImage imageNamed:@"home1"]];
    [m_scrollView addSubview:buttonView];
    
    [m_scrollView setContentSize:CGSizeMake(self.view.frame.size.width, buttonView.frame.origin.y + buttonView.frame.size.height + 160)];
}

- (void)loginSuccess{
    [self getHomeinfo];
}

- (void)getHomeinfo{
    SPHTTPRequestOperationManager *manager = [SPHTTPRequestOperationManager manager];
    NSDictionary * para = [[NSDictionary alloc]initWithObjectsAndKeys:USER_ID,@"user_id",INVITATION_CODE,@"invitation_code",PHONE,@"phone", nil];
    [manager getHomeInfoPara:para Success:^(AFHTTPRequestOperation *operation, id responseObject) {
        responseObject = [Methods safeResponseObj:responseObject];
        NSLog(@"get_home_info>>>>>>>>SUCCESS: %@",responseObject);
        //success
        if ([[responseObject valueForKey:@"is_success"] intValue] == 1) {
            [[NSUserDefaults standardUserDefaults] setValue:[[responseObject valueForKey:@"user"] valueForKey:@"account_name"] forKey:@"account_name"];
            [[NSUserDefaults standardUserDefaults]setValue:[[responseObject valueForKey:@"stat"] valueForKey:@"hhr_level"] forKey:@"user_rank"];
            int partnerCount = [[[responseObject valueForKey:@"stat"] valueForKey:@"firstly_partner_num"] intValue] + [[[responseObject valueForKey:@"stat"] valueForKey:@"secondary_partner_num"] intValue];
            [m_userInfoTextView setText:[NSString stringWithFormat:@"会员ID：%@ \n昵称：%@ \n等级：%@ \n下级合伙人数：%@ \n返还系数A：%%%@ \n返还系数B：%%%@",
                                         USER_ID,
                                         NICK_NAME,
                                         [[responseObject valueForKey:@"stat"] valueForKey:@"hhr_level"],
                                         [NSString stringWithFormat:@"%d",partnerCount],
                                         [[responseObject valueForKey:@"stat"] valueForKey:@"interest_return_coefficient"],
                                         [[responseObject valueForKey:@"stat"] valueForKey:@"charges_return_coefficient"]]];
            
            [earningTextField setText:[NSString stringWithFormat:@"日增收益：%@元   本月收益：%@元",[[responseObject valueForKey:@"stat"] valueForKey:@"daily_income"],[[responseObject valueForKey:@"stat"] valueForKey:@"monthly_income"]]];
        }
        else{
            [self showHintProgressWithText:[responseObject valueForKey:@"message"]];
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"get_home_info>>>>>>>>Error: %@", error);
    }];
}

@end
